﻿using Sidvall.Security;
using System.Collections.Generic;
using System.Security.Principal;
using System.Threading.Tasks;

namespace Sidvall.Web.Security
{
    public class UserEmailValidator : IIdentityValidator
    {
        #region Public Members

        #region AuthorityUserInfoEndpoint

        public string AuthorityUserInfoEndpoint
        {
            get
            {
                return this.Validator.AuthorityUserInfoEndpoint;
            }
            set
            {
                this.Validator.AuthorityUserInfoEndpoint = value;
            }
        }

        #endregion
        #region IgnoredUsers

        public System.Collections.ObjectModel.Collection<string> IgnoredUsers
        {
            get
            {
                return this.Validator.IgnoredUsers;
            }
        }

        #endregion

        #region ValidateAsync

        public async Task<IdentityValidationResult> ValidateAsync(IIdentity identity)
        {
            if (await this.Validator.IsValidAsync(identity, System.Web.HttpContext.Current.Request.Headers, System.Web.HttpContext.Current.Request.Form))
            {
                return new IdentityValidationResult()
                {
                    IdentityStatus = Sidvall.Security.IdentityStatus.Valid,
                };
            }
            else
            {
                return new IdentityValidationResult()
                {
                    IdentityStatus = Sidvall.Security.IdentityStatus.IncompleteRegistration,
                    ReasonPhrase = Sidvall.Resources.ResourceKeyManager.Current.ReasonPhraseEmailNotVerified,
                };
            }
        }

        #endregion

        #endregion
        #region Private Members

        private EmailValidator Validator { get; set; }

        #endregion
        #region Constructors

        public UserEmailValidator()
            : this(null, null)
        {
        }
        public UserEmailValidator(string authorityUserInfoEndpoint)
            : this(authorityUserInfoEndpoint, null)
        {
        }
        public UserEmailValidator(string authorityUserInfoEndpoint, IEnumerable<string> ignoredUsers)
        {
            this.Validator = new EmailValidator(authorityUserInfoEndpoint, ignoredUsers);
        }

        #endregion
    }
}
